package com.hockeo.shared.rpc.command;

/**
 * Special callback that invokes a registered {@link IResponseReceiver} when it has got
 * the response of a command. This can be used to avoid creating anonymous classes for
 * every submitted command.
 *
 * @version $Id$
 * @author jjanke
 */
public class ReceiverCallback<T extends IResponse> extends CommandCallback<T>
{
  private final IResponseReceiver<T> d_receiver;
  private final String               d_strInfo;

  /**
   * Creates a new receiver callback.
   *
   * @see #get(IResponseReceiver, String)
   */
  public ReceiverCallback( IResponseReceiver<T> receiver, String strInfo )
  {
    d_receiver = receiver;
    d_strInfo = strInfo;
  }

  /**
   * Convenience method to quickly create a new ReceiverCallback without having to write a
   * long 'new' statement.
   *
   * @param receiver the receiver to be notified once a command response has been received
   * @param strInfo an additional info that may have been submitted when a
   *          ReceiverCallback has been created. It can be used to differentiate between
   *          multiple commands returning the same type of response.
   */
  public static <T extends IResponse> ReceiverCallback<T> get( IResponseReceiver<T> receiver, String strInfo )
  {
    return new ReceiverCallback<T>( receiver, strInfo );
  }

  @Override
  public void got( T response )
  {
    d_receiver.gotResponse( response, d_strInfo );
  }
}
